package com.mercycrazy.springframework.jdbc.support;

import cn.hutool.core.lang.Assert;
import com.mercycrazy.springframework.beans.factory.InitializingBean;

import javax.sql.DataSource;

/**
 * Base class for {@link com.mercycrazy.springframework.jdbc.core.JdbcTemplate} and
 * other JDBC-accessing DAO helpers, defining common properties such as DataSource and exception translator.
 *
 * @author <a href="mailto:1443424326@qq.com">mercycrazy</a>
 * @since 2022-12-04
 */
public abstract class JdbcAccessor implements InitializingBean {

    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    protected DataSource obtainDataSource() {
        DataSource dataSource = getDataSource();
        Assert.state(dataSource != null, "No DataSource Set");
        return dataSource;
    }

    @Override
    public void afterProperties() {
        if (getDataSource() == null) throw new IllegalArgumentException("Property 'dataSource' is required");
    }
}
